package Heap;

import java.util.Comparator;
import java.util.PriorityQueue;

public class PriorityQueueTest {
    public static void main(String[] args) {
        int[] data={1,4,3,2,7,6,5,8,10,9};
        PriorityQueue queue=new PriorityQueue<>(new StudentCom());
        Student stu1=new Student("鲁智深",40);
        Student stu2=new Student("李逵",20);
        Student stu3=new Student("宋江",18);
        queue.offer(stu1);
        queue.offer(stu2);
        queue.offer(stu3);
        while(!queue.isEmpty()){
            System.out.println( queue.poll());
        }
//        Student[] arr={stu1,stu2,stu3};
//        Arrays.sort(arr,new StudentComDecs()); //默认升序
//        System.out.println(Arrays.toString(arr));

//        System.out.println(stu1.compareTo(stu2));
//        System.out.println(stu3.compareTo(stu2));
//        System.out.println(stu1.equals(stu2));

//        queue.offer(stu1);
//        queue.offer(stu2);
//        queue.offer(stu3);
//        System.out.println(queue.poll());

//        for(int i:data){
//            queue.offer(i);
//        }
//        int[] ret=new int[data.length];
//        for (int i = 0; i < ret.length; i++) {
//            ret[i]=queue.poll();
//        }
//        System.out.println(Arrays.toString(ret));
    }
}

class StudentCom implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge()-o2.getAge();
    }
}
class StudentComDecs implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o2.getAge()-o1.getAge();
    }
}

class Student {
    String name;
    int age;

    public int getAge() {
        return age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    //将当前对象和传入对象进行比较
    public boolean equals(Object o){
        if(o==null){
            return false;
        }
        if(this==o){
            return true;
        }
        //传入的o是否是Student类new出来的
        if(!(o instanceof Student)){
            //此时o不是Student类型，没有可比性
            return false;
        }
        //向下转型还原为Student类(比较Student类中的name和age属性，这两属性是Student类特有的，就需要向下转型还原为Student)
        Student stu=(Student)  o;
        return this.age==stu.age&&this.name.equals(stu.name);
    }

}